var logger = require('pomelo-logger').getLogger('bearcat-treasures', 'Player');
var bearcat = require('bearcat');
var util = require('util');

/**
 * Initialize a new 'Player' with the given 'opts'.
 * Player inherits Character
 *
 * @param {Object} opts
 * @api public
 */


function Player(opts) {
  this.opts = opts;
  this.id = opts.id;
  this.type = null;
  this.name = opts.name;
  this.walkSpeed = 240;
  this.score = opts.score || 0;
  this.target = null;
}

Player.prototype.init = function () {
  this.type = this.consts.EntityType.PLAYER;
  var Entity = bearcat.getFunction('entity');
  Entity.call(this, this.opts);
  this._init();
}

Player.prototype.addScore = function (score) {
  this.score += score;
};

/**
 * Parse String to json.
 * It covers object' method
 *
 * @param {String} data
 * @return {Object}
 * @api public
 */
Player.prototype.toJSON = function () {
  var r = this._toJSON();
  r['id'] = this.id;
  return r;
};

Player.prototype.sendMessageToPlayer = function (msg) {
  this.areaService.getChannelService().pushMessageByUids("onCmd", msg, [{ uid: this.id, sid: this.serverId }]);
}

Player.prototype.sendMessageToAll = function (msg) {
  this.areaService.getChannel().pushMessage("onCmd", msg);
}


Player.prototype.cmdstartGame = function (msg, session, next) {
  var selectGroupIndex = this.playerData.selectGroupIndex;
  var pThis = this;
  var mongoDbInstance = bearcat.getBean("mongoDb");

  mongoDbInstance.selectData("itembox", { id: selectGroupIndex }, function (err, resData) {
    if (pThis.playerData.money >= resData[0].needmoney) {
      msg.param.enoughMoney = true;
      pThis.playerData.money = pThis.playerData.money - resData[0].needmoney;
      msg.param.playerLastMoney = pThis.playerData.money;
      mongoDbInstance.updateData("players", { playerid: pThis.playerData.playerid }, { $set: { money: pThis.playerData.money } }, function (err, res) {
      });
    }
    else {
      msg.param.playerLastMoney = pThis.playerData.money;
      msg.param.enoughMoney = false;
    }
    
    pThis.sendMessageToPlayer(msg);
  });
}

Player.prototype.cmdScreenPrint = function (msg, session, next) {
  console.log("cmdScreenPrint " + JSON.stringify(msg));

  this.sendMessageToAll(msg);
}


//MongoDb.prototype.selectData = function (tableName, whereStr, callback) {
//MongoDb.prototype.updateData = function (tableName, whereStr, updateStr, callback) {

Player.prototype.cmdCheckCanFallen = function (msg, session, next) {
  var pThis = this;
  var wawaid = msg.param.wawaid;
  var mongoDbInstance = bearcat.getBean("mongoDb");
  mongoDbInstance.selectData("baseitem", { id: wawaid }, function (err, resData) {

    var pickProbability = resData[0].pickProbability;
    var addProbability = resData[0].addProbability;
    msg.param.luckvalue = 0;
    if (Math.floor(Math.random() * 1000) < pickProbability) {
      msg.param.isgetit = true;
      //抓住后还原几率为0
      mongoDbInstance.updateData("baseitem", { id: wawaid }, { $set: { pickProbability: 0 } }, function (err, res) {
      })
      pThis.playerData.luck = 0;
    }
    else if (pThis.playerData.luck >= 100) {//幸运值大于100直接抓住
      msg.param.isgetit = true;
      pThis.playerData.luck = 0;
    }
    else {
      //没抓住几率累加
      msg.param.isgetit = false;
      pickProbability += addProbability;
      mongoDbInstance.updateData("baseitem", { id: wawaid }, { $set: { pickProbability: pickProbability } }, function (err, res) {
      });
      var w = resData[0].addLuck[1] - resData[0].addLuck[0];
      var luckvalue = Math.round(Math.random() * w + resData[0].addLuck[0]);
      pThis.playerData.luck += luckvalue;
      if (pThis.playerData.luck > 100) {
        pThis.playerData.luck = 100;
      }
      msg.param.luckvalue = pThis.playerData.luck;
    }

    pThis.sendMessageToPlayer(msg);
    mongoDbInstance.updateData("players", { playerid: pThis.playerData.playerid }, { $set: { luck: pThis.playerData.luck } }, function (err, res) {

    });
  })
}

module.exports = {
  id: "player",
  func: Player,
  scope: "prototype",
  parent: "entity",
  init: "init",
  args: [{
    name: "opts",
    type: "Object"
  }],
  props: [{
    name: "consts",
    ref: "consts"
  }, {
    name: "areaService",
    ref: "areaService"
  }]
}

/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*/